<?php

/**
 * lzp300 2015-01-27
 */
class unify extends actionAbstract {

    /**
     * 构造
     */
    function __construct() {
        parent::__construct();
        if (!isset($_COOKIE['qingMg_admininfo'])) {
            header('location:' . $this->url('common/login'));
        }
    }

    //订单管理
    public function order() {
        global $myconfigs;
        $this->loadModel('qm', 'orders');
        $this->loadModel('qm', 'channels');
		
		$channels=$this->qm->channelsModel->fetchAll("select osiiid,title from qm_channels");
		
        $page = isset($_GET['page']) ? ((int) ($_GET['page']) ? (int) ($_GET['page']) : 1) : 1;
        $keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';

        $start = isset($_GET['start']) ? trim($_GET['start']) : '';
        $end = isset($_GET['end']) ? trim($_GET['end']) : '';
        if ($start && $end && (strtotime($end) < strtotime($start))) {
            $start = $_GET['end'];$end = $_GET['start'];
        }
		$step=isset($_GET['step'])?(int)($_GET['step']):0;
		$orderId=isset($_GET['orderId'])?trim($_GET['orderId']):'';
		$hotelName=isset($_GET['hotelName'])?trim($_GET['hotelName']):'';
		$uname=isset($_GET['uname'])?trim($_GET['uname']):'';
		$qmorderId=isset($_GET['qmorderId'])?trim($_GET['qmorderId']):'';
		$chann=isset($_GET['chann'])?(int)($_GET['chann']):0;
		

        $sql_from = " from qm_orders as a left join qm_hotel as b on a.hotelId=b.hid ";
        $sql_from.="left join qm_room as c on c.hid=a.hotelId and c.rid=a.roomId ";
		$sql_from.="left join qm_channels as d on d.osiiid=a.type";
        $sql_where = " where a.id>0";
        $sql_order_by = " order by a.created desc";
        $field_reto = " count(*)";
        $field_re = " a.*,b.name as hotelName,c.name as title,d.title as chanName ";
		
		if($step==1){
			if($start){$sql_where.=" and a.created>='".$start."'";}
			if($end){$sql_where.=" and a.created<='".$end."'";}
		}elseif($step==2){
			if($start){$sql_where.=" and a.startDate>='".$start."'";}
			if($end){$sql_where.=" and a.startDate<='".$end."'";}
		}elseif($step==3){
			if($start){$sql_where.=" and a.endDate>='".$start."'";}
			if($end){$sql_where.=" and a.endDate<='".$end."'";}
		}
		if($orderId){$sql_where.=" and a.orderId='".$orderId."'";}
		if($hotelName){$sql_where.=" and b.name like'%".$hotelName."%'";}
		if($uname){$sql_where.=" and a.name like '%".$uname."%'";}
		if($qmorderId){$sql_where.=" and a.qmorderId='".$qmorderId."'";}
		if($chann){$sql_where.=" and a.type='".$chann."'";}

        $return = $this->getPage($sql_from, $sql_where, $sql_order_by, $field_reto, $field_re, "qm", "orders", $page, 10);
        $re = $return['re'];
        $number = $return['number'];
        $pageHtml = $return['pageHtml'];

        include $this->loadWidget('masterTheme');
    }
	
	//取消订单
	function CancelHotelOrder(){
		$kid = isset($_POST['kid']) ? (int) ($_POST['kid']) : 0;
        $this->loadModel('qm', 'orders');
		$date=array('status'=>99);
        $re = $this->qm->ordersModel->update($date,"id=$kid");
        if ($re) {
            exit(json_encode(array('state' => 1, 'html' => '取消成功，2秒后自动刷新')));
        } else {
            exit(json_encode(array('state' => 0, 'html' => '取消失败')));
        }
	}
	
	//修改备注
	function upRemark(){
		$kid = isset($_POST['kid']) ? (int) ($_POST['kid']) : 0;
		$remark = isset($_POST['remark']) ? $_POST['remark'] : '';
        $this->loadModel('qm', 'orders');
		$date=array('remark'=>$remark);
        $re = $this->qm->ordersModel->update($date,"id=$kid");
        if ($re) {
            exit(json_encode(array('state' => 1, 'html' => '修改成功，2秒后自动刷新')));
        } else {
            exit(json_encode(array('state' => 0, 'html' => '修改失败')));
        }
	}
	
	//导出订单
	function texport(){
		global $myconfigs;
		$this->loadModel('qm','orders');
		$step=isset($_GET['step'])?(int)($_GET['step']):0;
		$orderId=isset($_GET['orderId'])?trim($_GET['orderId']):'';
		$hotelName=isset($_GET['hotelName'])?trim($_GET['hotelName']):'';
		$uname=isset($_GET['uname'])?trim($_GET['uname']):'';
		$qmorderId=isset($_GET['qmorderId'])?trim($_GET['qmorderId']):'';
		$chann=isset($_GET['chann'])?(int)($_GET['chann']):0;
		
		$start = isset($_GET['start']) ? trim($_GET['start']) : '';
		$end = isset($_GET['end']) ? trim($_GET['end']) : '';
        if ($start && $end && (strtotime($end) < strtotime($start))) {
            $start = $_GET['end'];$end = $_GET['start'];
        }
		
		$sql="select a.*,b.name as hotelName,c.name as title,d.title as chanName from qm_orders as a left join qm_hotel as b on b.hid=a.hotelId";
		$sql.=" left join qm_room as c on c.hid=a.hotelId and c.rid=a.roomId left join qm_channels as d on d.osiiid=a.type where a.id>0";
		
		if($step==1){
			if($start){$sql.=" and a.created>='".$start."'";}
			if($end){$sql.=" and a.created<='".$end."'";}
		}elseif($step==2){
			if($start){$sql.=" and a.startDate>='".$start."'";}
			if($end){$sql.=" and a.startDate<='".$end."'";}
		}elseif($step==3){
			if($start){$sql.=" and a.endDate>='".$start."'";}
			if($end){$sql.=" and a.endDate<='".$end."'";}
		}
		if($orderId){$sql.=" and a.orderId='".$orderId."'";}
		if($hotelName){$sql.=" and b.name like'%".$hotelName."%'";}
		if($uname){$sql.=" and a.name like '%".$uname."%'";}
		if($qmorderId){$sql.=" and a.qmorderId='".$qmorderId."'";}
		if($chann){$sql.=" and a.type='".$chann."'";}
		
		$re=$this->qm->ordersModel->fetchAll($sql);
		
		$title="订单表".date("Y-m-d_H-i-s",time());
        $filed=array('订单号','青芒果订单号','渠道','酒店名称','房型名称','入住人','电话','入住时间','离店时间','订单金额','佣金','间/夜','状态','下单时间','备注');
        $result = array();
		foreach($re as $k=>$val){
            $result[$k]['orderId']	      =	$val['orderId']." ";
            $result[$k]['qmorderId']	  =	$val['qmorderId']." ";
            $result[$k]['chanName']	  =	$val['chanName'];
            $result[$k]['hotelName']  =	$val['hotelName'];
            $result[$k]['title']	  =	$val['title'];
            $result[$k]['name']    =	$val['name'];
            $result[$k]['mobile']    =	$val['mobile'];
            $result[$k]['startDate'] =	$val['startDate'];
            $result[$k]['endDate']       =	$val['endDate'];
            $result[$k]['price']  =	$val['price'];
            $result[$k]['commission']	  =	$val['commission'];
            $result[$k]['jyd']      =	$val['countRoom'].'/'.((strtotime($val['endDate']) - strtotime($val['startDate']))/24/3600);
            $result[$k]['status']    =	$myconfigs['status'][$val['status']];
            $result[$k]['created']    =	$val['created'];
            $result[$k]['remark']    =	$val['remark'];
		}
        $this->outExcel($title,$filed,$result);
	}
	
	//新酒店操作区
	function selectHotel(){
		include $this->loadWidget('masterTheme');
	}
	//新酒店
	function newHotel(){
		set_time_limit(0);
		$this->loadModel('qm','hotel');
		$this->loadModel('qm','uphotel');//阿里
		$this->loadModel('xc','uphotel');//携程
		$this->loadModel('kall','uphotel');
		$this->loadApi('orderapi');
		$orderapi = new orderapi();
		
		//获取可售酒店
		$string="";
		$GetImmediateHotelList=$orderapi->GetImmediateHotelListTO();
		foreach($GetImmediateHotelList as $k=>$v){
			$as=$this->qm->hotelModel->fetchRow("select id from qm_hotel where id>=3074 and hid='".$v."'");
			if(!$as){
				$string.=",".$v;$data['hid']=$v;
				$this->qm->hotelModel->insert($data);
			}
		}
		$string=trim($string,',');
		exit(json_encode(array('state' => 1, 'html' => '新酒店ID：'.$string)));
	}
	//上架新酒店到阿里
	function toAli(){
		set_time_limit(0);
		$this->loadModel('qm','hotel');
		$this->loadModel('qm','uphotel');//阿里
		$string=isset($_POST['string'])?$_POST['string']:'';
		$stringArr=explode(',',$string);
		$a=0;
		foreach($stringArr as $k=>$v){
			$rs=$this->qm->hotelModel->fetchRow("select id from qm_hotel where hid='".$v."' and `name`!='' and osiiCityID>0 and osiiCountyID>0 and osiiProvinceID>0 and tell is not null");
			if($rs){
				$data['hid']=$v;$data['status']=1;$data['created']=date('Y-m-d H:i:s',time());
				$re=$this->qm->uphotelModel->insert($data);
				if($re){$a++;}
			}
		}
		exit(json_encode(array('state' => 1, 'html' => '上架了 '.$a.' 个酒店到阿里')));
	}
	//上架新酒店到携程
	function toXiecheng(){
		set_time_limit(0);
		$this->loadModel('qm','hotel');
		$this->loadModel('xc','uphotel');//携程
		$string=isset($_POST['string'])?$_POST['string']:'';
		$stringArr=explode(',',$string);
		$a=0;
		foreach($stringArr as $k=>$v){
			$rs=$this->qm->hotelModel->fetchRow("select id from qm_hotel where hid='".$v."' and `name`!='' and osiiCityID>0 and osiiCountyID>0 and osiiProvinceID>0");
			if($rs){
				$data['hid']=$v;$data['status']=1;$data['created']=date('Y-m-d H:i:s',time());
				$re=$this->xc->uphotelModel->insert($data);
				if($re){$a++;}
			}
		}
		exit(json_encode(array('state' => 1, 'html' => '上架了 '.$a.' 个酒店到携程')));
	}
	
}